<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;

class Articletype extends cmsBase {

    public function index(){
        if(empty($_GET['name'])){
            $type = Db::name('cms_type')->where('status','not in',2)->paginate(10);
        }else{
            $type = Db::name('cms_type')
                ->where('typename','like',"%{$_GET['name']}%")
                ->where('status','not in','2')
                ->paginate(10,false,['query'=>$_GET]);
        }

        $page = $type->render();
        return view('article/type',['type'=>$type,'page'=>$page]);
    }

    public function typeView(){
        return view('article/addtype');
    }

    public function addType(){
        $param = $this->request->post();
        $type = Db::name('cms_type')->where('typename',$param['name'])->where('status','not in',2)->find();
        if(!$type){
            $data = [
                'typename'=>$param['name'],
                'cretatetime'=>date('Y-m-d H:i:s'),
                'status'=>$param['status'],
            ];
            $in = Db::name('cms_type')->insert($data);
            if($in){
                return "<script>alert('新增成功');parent.location.reload();</script>";
            }else{
                return "<script>alert('添加失败');history.go(-1)</script>";
            }
        }else{
            return "<script>alert('添加失败');history.go(-1)</script>";
        }
    }

    public function edit(){
        if(empty($_POST['id'])){
            $type = Db::name('cms_type')->where('id',$_GET['id'])->find();
            return view('article/edittype',['type'=>$type]);
        }else{
            $param = $this->request->post();
            dump($_POST);
            $data = [
                'typename'=>$param['name'],
                'status'=>$param['status'],
            ];
            $up = Db::name('cms_type')->where('id',$param['id'])->update($data);
            if($up){
                return "<script>alert('修改成功');parent.location.reload();</script>";
            }else{
                return "<script>alert('添加失败');history.go(-1)</script>";
            }
        }

    }
}